﻿using System;
using System.Data;
using System.Data.OleDb;
using System.Text;
using CardRegisterCollection.DBUtility;
using CardRegisterCollection.Model;

//Please add references
namespace CardRegisterCollection.DAL
{
    /// <summary>
    /// 数据访问类:unit
    /// </summary>
    public partial class Unit
    {
        public Unit()
        { }
        #region  BasicMethod

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperOleDb.GetMaxID("ID", "unit");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from unit");
            strSql.Append(" where DeleteFlag = 0 ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(strWhere);
            }
            return DbHelperOleDb.Exists(strSql.ToString());
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Model.Unit model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into unit(");
            strSql.Append("ParentId,Name,Type,CreatedBy,CreatedAt,DeleteFlag,ID)");
            strSql.Append(" values (");
            strSql.Append("@ParentId,@Name,@Type,@CreatedBy,@CreatedAt,@DeleteFlag,@ID)");
            OleDbParameter[] parameters = {
                    new OleDbParameter("@ParentId", OleDbType.VarChar,255),
                    new OleDbParameter("@Name", OleDbType.VarChar,255),
                    new OleDbParameter("@Type", OleDbType.Integer,4),
                    new OleDbParameter("@CreatedBy", OleDbType.VarChar,255),
                    new OleDbParameter("@CreatedAt", OleDbType.Date),
                    new OleDbParameter("@DeleteFlag", OleDbType.Boolean,1),
                    new OleDbParameter("@ID", OleDbType.VarChar,255),
            };
            parameters[0].Value = model.ParentId;
            parameters[1].Value = model.Name;
            parameters[2].Value = model.Type;
            parameters[3].Value = model.CreatedBy;
            parameters[4].Value = model.CreatedAt;
            parameters[5].Value = model.DeleteFlag;
            parameters[6].Value = model.ID;

            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.Unit model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update [unit] set ");
            strSql.Append("Name=@Name,");
            strSql.Append("LastUpdatedBy=@LastUpdatedBy,");
            strSql.Append("LastUpdatedAt=@LastUpdatedAt");
            strSql.Append(" where ID=@ID");
            OleDbParameter[] parameters = {
                    new OleDbParameter("@Name",OleDbType.VarChar,255),
                    new OleDbParameter("@LastUpdatedBy", OleDbType.VarChar,255),
                    new OleDbParameter("@LastUpdatedAt", OleDbType.Date),
                    new OleDbParameter("@ID",   OleDbType.VarChar,255)};

            parameters[0].Value = model.Name;
            parameters[1].Value = CurrentLoginInfo.UserInfo.UserName;
            parameters[2].Value = DateTime.Now;
            parameters[3].Value = model.ID;

            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from unit ");
            strSql.Append(" where ID=@ID");
            OleDbParameter[] parameters = {
                    new OleDbParameter("@ID",OleDbType.VarChar,255)
            };
            parameters[0].Value = ID;

            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.Unit GetModel(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,ParentId,Name,Type,CreatedBy,CreatedAt,DeleteFlag,LastUpdatedBy,LastUpdatedAt from unit ");
            strSql.Append(" where ID=@ID");
            OleDbParameter[] parameters = {
                    new OleDbParameter("@ID", OleDbType.Integer,4)
            };
            parameters[0].Value = ID;

            Model.Unit model = new Model.Unit();
            DataSet ds = DbHelperOleDb.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.Unit DataRowToModel(DataRow row)
        {
            Model.Unit model = new Model.Unit();
            if (row != null)
            {
                if (row["ID"] != null && row["ID"].ToString() != "")
                {
                    model.ID = row["ID"].ToString();
                }
                if (row["ParentId"] != null)
                {
                    model.ParentId = row["ParentId"].ToString();
                }
                if (row["Name"] != null)
                {
                    model.Name = row["Name"].ToString();
                }
                if (row["Type"] != null && row["Type"].ToString() != "")
                {
                    model.Type = int.Parse(row["Type"].ToString());
                }
                if (row["CreatedBy"] != null)
                {
                    model.CreatedBy = row["CreatedBy"].ToString();
                }
                if (row["CreatedAt"] != null && row["CreatedAt"].ToString() != "")
                {
                    model.CreatedAt = DateTime.Parse(row["CreatedAt"].ToString());
                }
                if (row["DeleteFlag"] != null && row["DeleteFlag"].ToString() != "")
                {
                    if ((row["DeleteFlag"].ToString() == "1") || (row["DeleteFlag"].ToString().ToLower() == "true"))
                    {
                        model.DeleteFlag = true;
                    }
                    else
                    {
                        model.DeleteFlag = false;
                    }
                }
                if (row["LastUpdatedBy"] != null)
                {
                    model.LastUpdatedBy = row["LastUpdatedBy"].ToString();
                }
                if (row["LastUpdatedAt"] != null && row["LastUpdatedAt"].ToString() != "")
                {
                    model.LastUpdatedAt = DateTime.Parse(row["LastUpdatedAt"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,ParentId,Name,Type,CreatedBy,CreatedAt,DeleteFlag,LastUpdatedBy,LastUpdatedAt ");
            strSql.Append(" FROM unit ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperOleDb.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM unit ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.ID desc");
            }
            strSql.Append(")AS Row, T.*  from unit T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperOleDb.Query(strSql.ToString());
        }

        /*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			OleDbParameter[] parameters = {
					new OleDbParameter("@tblName", OleDbType.VarChar, 255),
					new OleDbParameter("@fldName", OleDbType.VarChar, 255),
					new OleDbParameter("@PageSize", OleDbType.Integer),
					new OleDbParameter("@PageIndex", OleDbType.Integer),
					new OleDbParameter("@IsReCount", OleDbType.Boolean),
					new OleDbParameter("@OrderType", OleDbType.Boolean),
					new OleDbParameter("@strWhere", OleDbType.VarChar,1000),
					};
			parameters[0].Value = "unit";
			parameters[1].Value = "ID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperOleDb.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}

